package com.hngy.xpq.persondemo.adapter;

import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.hngy.xpq.persondemo.R;
import com.hngy.xpq.persondemo.assets.XpqIcoMoonUtils;
import com.hngy.xpq.persondemo.bean.Op;

import java.util.List;

public class ListAdapter extends RecyclerView.Adapter<ListAdapter.ViewHolder> {

    private List<Op> listData;
//    private is

    static class ViewHolder extends RecyclerView.ViewHolder {

        View fileView;
        TextView name, icon;

        public ViewHolder(View itemView) {
            super(itemView);
            fileView = itemView;
            icon = itemView.findViewById(R.id.icon);
            name = itemView.findViewById(R.id.name);
            XpqIcoMoonUtils.setIOC(icon);
        }
    }

    // 获取到数据
    public ListAdapter(List<Op> list) {
        this.listData = list;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull final ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list,parent,false);
        final ViewHolder holder = new ViewHolder(view);
        holder.fileView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 修改背景
                holder.fileView.setBackgroundResource(R.color.ff);
                // 跳转
                int position = holder.getAdapterPosition();
                Intent intent = new Intent(parent.getContext(), (Class<?>) listData.get(position).getCla());
                parent.getContext().startActivity(intent);
            }
        });
        holder.fileView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        holder.fileView.setBackgroundResource(R.color.f0);
                        break;
                    case MotionEvent.ACTION_UP:
                        Log.i("data", "松开");
                        break;
                    default:
                        holder.fileView.setBackgroundResource(R.color.ff);
                        break;
                }
                return false;
            }
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        // 数据绑定
        final Op data = listData.get(position);
        holder.icon.setText(data.getIcon());
        holder.name.setText(data.getName());
    }

    @Override
    public int getItemCount() {
        return listData.size();
    }

}
